package com.leigang.java.code.mulithread.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author leig-a
 */
public class SellTicketLock {
    /**
     *  有100张票
     */
    private int tickets = 100;

    Lock lock = new ReentrantLock();

    /**
     * 买票方法，模拟买票的动作
     */
    public void sell() {
        lock.lock();
        try {
            if (tickets > 0) {
                System.out.println(Thread.currentThread().getName() + "，" +
                        "已经卖出了第（" + tickets-- + "）张票，" +
                        "还剩（" + tickets + "）张票。");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }
}
